package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.business.query.CreditTransferQuery;
import cn.wolfcode.p2p.business.service.ICreditTransferService;
import cn.wolfcode.p2p.util.JsonResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CreditTransferController {
    @Autowired
    private ICreditTransferService creditTransferService;

    @RequestMapping("credittransfer_list")
    public String creditTransferList(@ModelAttribute("qo") CreditTransferQuery qo, Model model) {
        //只查询当前登录用户的债权列表
        qo.setLoginInfoId(UserContext.getCurrentUser().getId());
        model.addAttribute("pageInfo", creditTransferService.queryCanCreditTransfer(qo));
        return "credittransfer/credittransfer_cantrans_list";
    }


    /**
     * 执行债权转让
     */
    @RequestMapping("creditTransfer")
    @ResponseBody
    public JsonResult creditTransfer(Long[] bidId){
        JsonResult jsonResult = new JsonResult();
        try{
            creditTransferService.creditTransfer(bidId);
        }catch (CustomerException e){
            jsonResult.setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMessage("啊,系统异常啦,我们正在处理中,请联系客服");
        }
        return jsonResult;
    }
}
